package com.example.demo.nettydemo.util;

import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SerializationUtil {


    private final static Map<Class<?>, Schema<?>> schemaCache = new ConcurrentHashMap<>();


    public static <T> T deserialize(Class<T> cls,byte[] data){
        Schema<T> schema = getSchema(cls);
        T message = schema.newMessage();

//        ProtostuffIOUtil.mergefrom

    }


    public static <T> Schema<T> getSchema(Class<T> cls){
        Schema<T> schema = (Schema<T>) schemaCache.get(cls);
        if(schema == null){
            schema = RuntimeSchema.getSchema(cls);
            schemaCache.put(cls,schema);
        }
        return schema;
    }


    public static <T> byte[] serialize(T object){
        LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
        try {
            Class<T> cls = (Class<T>) object.getClass();
            Schema<T> schema =

        }catch (Exception e){
            throw e;
        }finally {
            buffer.clear();
        }
    }
}
